package com.sunny.ynhz.ui.login;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sunny.ynhz.R;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.ChangePasswordBean;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.ShowUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by luao on 2017/7/24.
 */

public class ChangePasswordActivity extends AppBaseTranslationTitleBarActivity {

    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.confirm_new_password)
    EditText confirmNewPassword;
    @BindView(R.id.change_password)
    Button changePassword;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepassword);
        ButterKnife.bind(this);
        activity = this;


        oldPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });

        newPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        confirmNewPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });

    }

    @OnClick(R.id.change_password)
    public void onClick(View view) {
        if (oldPassword.getText().toString().equals("")) {
            Toast.makeText(activity, "请输入旧密码", Toast.LENGTH_LONG).show();
            return;
        } else if (newPassword.getText().toString().equals("")) {
            Toast.makeText(activity, "请输入新密码", Toast.LENGTH_LONG).show();
            return;
        } else if (confirmNewPassword.getText().toString().equals("")) {
            Toast.makeText(activity, "请确认新密码", Toast.LENGTH_LONG).show();
            return;
        } else if (!newPassword.getText().toString().equals(confirmNewPassword.getText().toString())) {
            Toast.makeText(activity, "两次输入密码不一致", Toast.LENGTH_LONG).show();
            return;
        } else {
            changePassword();
        }

    }


    private void changePassword() {
        String string = SPUtil.get("userId", "").toString();
        HttpManager.getInstance()
                .changePassword(SPUtil.get("login_name", "").toString(), oldPassword.getText().toString(), newPassword.getText().toString())
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiSubscriber(this, new ApiSubscriber.HttpOnResultListener<ChangePasswordBean>() {
                    @Override
                    public void onNext(ChangePasswordBean jsonObject) {
                        Toast.makeText(activity, "成功修改密码", Toast.LENGTH_LONG).show();
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        ShowUtils.closePopwindow();
                    }

                    @Override
                    public void onstart() {
                        ShowUtils.disPlayProgress(activity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        ShowUtils.closePopwindow();
                        super.onError(e);

                    }
                }));
    }
}
